package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.example.myapplication.Bean.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private Switch autoswitch;
    private EditText usnedit, psdedit;
    private Button registerbtn, loginbtn;
    private TextView logintxt;
    List<User> list = new ArrayList<>();
    int CHOOSE_LEVEL=0;
    int userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getUser();
        setListener();
    }

    private void initView() {
        radioGroup=findViewById(R.id.radiogroup);
        autoswitch=findViewById(R.id.autoswitch);
        usnedit = findViewById(R.id.usnedit);
        psdedit = findViewById(R.id.psdedit);
        registerbtn = findViewById(R.id.registerbtn);
        loginbtn = findViewById(R.id.loginbtn);
        logintxt = findViewById(R.id.logtxt);


    }


    private void setListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.adminbtn:
                    CHOOSE_LEVEL=1;
                    break;
                    case R.id.userbtn:
                        CHOOSE_LEVEL=2;
                        break;
                }
            }
        });



        autoswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()){
                    SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                    usnedit.setText(sp.getString("usename",""));
                    psdedit.setText(sp.getString("password",""));
                }else {
                    usnedit.setText("");
                    psdedit.setText("");
                }
            }
        });








        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login()) {
                    Intent intent=new Intent(MainActivity.this,SuccessActivity.class);
                    startActivityForResult(intent,1);
                    logintxt.setText("登陆成功，id为：" + userid);
                } else {
                    logintxt.setText("登录失败");
                }
            }
        });
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }
    private void getUser() {
        User user1 = new User(0, "张三", "111");
        User user2 = new User(1, "李四", "222");
        User user3 = new User(2, "王五", "333");
        list.add(user1);
        list.add(user2);
        list.add(user3);
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent intent){
        super.onActivityResult(requestCode,resultCode,intent);
        if(requestCode==1&& resultCode==2){
            String usn= intent.getStringExtra("username");
            String psd= intent.getStringExtra("password");
            register(usn,psd);
        }
    }

    private void register(String usn,String psd){
        boolean k=true;
        for (User u :list){
            if (usn.equals(u.getUsername())){
                k=false;
            }
        }
        if (k){
        User user=new User();
        user.setId(list.size());
        user.setUsername(usn);
        user.setPassword(psd);
        list.add(user);
        logintxt.setText("注册成功");
    }else {
            logintxt.setText("注册重复");
        }
    }
    private boolean login(){
        boolean k = false;
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (usnedit.getText().toString().equals(user.getUsername())
                    && psdedit.getText().toString().equals(user.getPassword())) {
                SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("username",user.getUsername());
                editor.putString("passsword",user.getPassword());
                editor.apply();
                userid = user.getId();
                k = true;
            }
        }
        return k;
    }
}
